### Out of Sight, Out of Mind? ###
### Figures in the Appendix ###


rm(list=ls())

library(magrittr) ## Version: 2.0.1
library(dplyr)  ## Version: 1.0.5
library(readstata13) ## Version: 0.9.2
library(interflex) ## Version: 1.2.5
library(ggplot2) ## Version: 3.3.3
library(gridExtra) ## Version: 2.3

set.seed(123456)


d <- read.dta13('./final_data.dta')


lograyic_models <- d %>% filter(year<2014) %>% select(rn, school_id_cluster, Dakp_3, Dwalk_3, lograyic09, Duniversity_3, Dodr_3, Dydr_3, Dpopulation_3, Dhospital_pri_3, Dhospital_pub_3)

lograyic_models <- lograyic_models[complete.cases(lograyic_models), ]

## Figure A1: Conditional effect of Dwalk on Dakp using kernel estimator ---- 

##The moderator is property values in 2009 (logged)

walk_lograyic_kernel <- interflex(Y = "Dakp_3", D = "Dwalk_3", X = "lograyic09", Z = c('Duniversity_3', 'Dodr_3', 'Dydr_3', 'Dpopulation_3', 'Dhospital_pri_3', 'Dhospital_pub_3'),data = lograyic_models, estimator = 'kernel', cl="rn", bw = 0.2065, main = "", Xlabel = "Property values (2009)", vcov.type = 'cluster', xlab = 'Moderator: Property values (2009)', ylab = 'Marginal effect of Dwalk on Dakp', cex.lab = 1, cex.axis = 1, theme.bw = TRUE, height=6, width = 4, file='./figure_A1.pdf', ylim = c(-0.45, 0.45))



## Figure A2: Conditional effect of Dcongestion on Dakp using kernel estimator ----

##The moderator is property values in 2009 (logged)

congestion_models <- d %>% filter(year<2014) %>%  select(rn, school_id_cluster, Dakp_3, Dpop1000_asm_dr_3, Dwalk_3, lograyic09, Duniversity_3, Dodr_3, Dydr_3, Dpopulation_3, Dhospital_pri_3, Dhospital_pub_3)

congestion_models <- congestion_models[complete.cases(congestion_models), ]


congestion_lograyic_kernel <- interflex(Y = "Dakp_3", D = "Dpop1000_asm_dr_3", X = "lograyic09", Z = c('Duniversity_3', 'Dodr_3', 'Dydr_3', 'Dpopulation_3', 'Dhospital_pri_3', 'Dhospital_pub_3'),data = congestion_models, estimator = 'kernel', cl="rn", bw = 0.2065, main = "", vcov.type = 'cluster', xlab = 'Moderator: Property values (2009)', ylab = 'Marginal effect of Dcongestion on Dakp', cex.lab = 1, cex.axis = 1, theme.bw = TRUE, height=6, width = 4, file='./figure_A2.pdf')


